home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / System7 tools / Frontier / Frontier SDK 2.1 / Toolkits / IAC Tools / iacsysevents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-20  |  2.8 KB  |  162 lines  |  [TEXT/KAHL]

  1.  
  2.  
  3. /*© Copyright 1988-1992 UserLand Software, Inc.  All Rights Reserved.*/
  4.  
  5.  
  6. #include "iacinternal.h"
  7.  
  8.  
  9. typedef struct tysystemhandler {
  10.     
  11.     AEEventClass eventclass;
  12.     
  13.     AEEventID id;
  14.     
  15.     tyAEHandler proc;
  16.     
  17.     long A5;
  18.     
  19.     THz heapzone;
  20.     
  21.     struct tysystemhandler **hnexthandler;
  22.     } tysystemhandler, **hdlsystemhandler;
  23.     
  24.  
  25. hdlsystemhandler hfirstsystemhandler = nil;
  26.  
  27.  
  28.  
  29.  
  30. static pascal OSErr callhandler (AppleEvent *event, AppleEvent *reply, long refcon) {
  31.  
  32.     hdlsystemhandler h = (hdlsystemhandler) refcon;
  33.     long appA5;
  34.     THz savedzone;
  35.     OSErr ec;
  36.     
  37.     appA5 = (**h).A5;
  38.     
  39.     asm { /*save all registers, set value of A5*/
  40.     
  41.         movem.l a0-a6/d0-d7, -(sp)
  42.         
  43.         move.l appA5, a5
  44.         }
  45.     
  46.     savedzone = GetZone ();
  47.     
  48.     SetZone ((**h).heapzone);
  49.     
  50.     ec = (*(**h).proc) (event, reply, 0L);
  51.     
  52.     SetZone (savedzone);
  53.     
  54.     asm { /*restore all registers*/
  55.     
  56.         movem.l (sp)+, a0-a6/d0-d7
  57.         }
  58.     
  59.     return (ec);
  60.     } /*callhandler*/
  61.     
  62.     
  63. Boolean IACinstallsystemhandler (AEEventClass eventclass, AEEventID id, ProcPtr handler) {
  64.     
  65.     OSErr ec;
  66.     hdlsystemhandler h;
  67.     
  68.     h = (hdlsystemhandler) NewHandle ((long) sizeof (tysystemhandler));
  69.     
  70.     if (h == nil)
  71.         return (false);
  72.         
  73.     (**h).eventclass = eventclass;
  74.     
  75.     (**h).id = id;
  76.         
  77.     (**h).proc = (tyAEHandler) handler;
  78.     
  79.     (**h).A5 = (long) CurrentA5;
  80.     
  81.     (**h).heapzone = GetZone ();
  82.     
  83.     ec = AEInstallEventHandler (eventclass, id, (EventHandlerProcPtr) callhandler, (long) h, true);
  84.     
  85.     IACglobals.errorcode = ec;
  86.  
  87.     if (ec != noErr) {
  88.         
  89.         DisposHandle ((Handle) h);
  90.         
  91.         return (false);
  92.         }
  93.         
  94.     (**h).hnexthandler = hfirstsystemhandler;
  95.     
  96.     hfirstsystemhandler = h;
  97.     
  98.     return (true);
  99.     } /*IACinstallsystemhandler*/
  100.     
  101.     
  102. Boolean IACremovesystemhandler (AEEventClass eventclass, AEEventID id, ProcPtr handler) {
  103.  
  104.     hdlsystemhandler h = hfirstsystemhandler;
  105.     hdlsystemhandler hprev = nil;
  106.     OSErr ec;
  107.     
  108.     while (h != nil) {
  109.         
  110.         if (((**h).eventclass == eventclass) && ((**h).id == id)) {
  111.         
  112.             ec = AERemoveEventHandler (eventclass, id, (EventHandlerProcPtr) callhandler, true);
  113.             
  114.             if (hprev == nil)
  115.                 hfirstsystemhandler = (**h).hnexthandler;
  116.             else
  117.                 (**hprev).hnexthandler = (**h).hnexthandler;
  118.                 
  119.             DisposHandle ((Handle) h);
  120.             
  121.             return (true);
  122.             }
  123.         
  124.         hprev = h;
  125.         
  126.         h = (**h).hnexthandler;
  127.         } /*while*/
  128.         
  129.     return (false);
  130.     } /*IACremovesystemhandler*/
  131.     
  132.     
  133. void IACremovesystemhandlers (void) {
  134.     
  135.     /*
  136.     remove all system event handlers that were installed by this application.
  137.     
  138.     3/4/93 DW: make it possible for this routine to be called two or more times.
  139.     */
  140.     
  141.     hdlsystemhandler h = hfirstsystemhandler;
  142.     hdlsystemhandler hnext;
  143.     OSErr ec;
  144.     
  145.     while (h != nil) {
  146.         
  147.         ec = AERemoveEventHandler ((**h).eventclass, (**h).id, (EventHandlerProcPtr) callhandler, true);
  148.         
  149.         IACglobals.errorcode = ec;
  150.         
  151.         hnext = (**h).hnexthandler;
  152.         
  153.         DisposHandle ((Handle) h);
  154.         
  155.         h = hnext;
  156.         } /*while*/
  157.         
  158.     hfirstsystemhandler = nil; /*3/4/93*/
  159.     } /*IACremovesystemhandlers*/
  160.     
  161.     
  162.